//
//  JJRefreshControl.swift
//  JJWeibo
//
//  Created by JJ on 15/9/9.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

private let JJRefreshControlMaxOffset:CGFloat = -60
class JJRefreshControl: UIRefreshControl {

   override func endRefreshing() {
        super.endRefreshing()
        refreshView.stopLoadingAnimation()
    }
 
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    
        if frame.origin.y > 0{
            return
        }
        
        if refreshing{
            
            refreshView.loadingAnimation()
            return
        }
        
        if frame.origin.y < JJRefreshControlMaxOffset && !refreshView.rotateFlag{
            print("转过去")
           refreshView.rotateFlag=true
        }else if frame.origin.y >= JJRefreshControlMaxOffset && refreshView.rotateFlag{
            print("反过来")
            refreshView.rotateFlag=false
            
        }
        
    }

    override init() {
        super.init()
        
        setUI()
        
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        self.removeObserver(self, forKeyPath: "frame")
    }
    
    private func setUI(){
        
        self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
        
        tintColor=UIColor.clearColor()
        
        addSubview(refreshView)
        
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
        
    }
    
    
    private lazy var refreshView:JJRefreshView=JJRefreshView.refreshView()

}


class JJRefreshView: UIView {
    
    private var rotateFlag = false {
        didSet{
            rotateTipAnimation()
            
        }
        
    }
    

    @IBOutlet weak var pullDownImageView: UIImageView!
    
    @IBOutlet weak var loadingImageView: UIImageView!

    @IBOutlet weak var tipView: JJRefreshView!
    
    
    private func rotateTipAnimation(){
        
         var angle:CGFloat = CGFloat(M_PI)
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            angle += self.rotateFlag ? -0.01 :0.01
           self.pullDownImageView.transform=CGAffineTransformRotate(self.pullDownImageView.transform,angle)
            
        }
        
    }
    private func loadingAnimation(){
        
        tipView.hidden=true
        if loadingImageView.layer.animationForKey("JJ") != nil {
            return
        }
        
        let ani=CABasicAnimation(keyPath: "transform.rotation")
        ani.duration=2
        ani.repeatCount=MAXFLOAT
        ani.toValue=M_PI * 2
        
        loadingImageView.layer.addAnimation(ani, forKey: "JJ")
        
    }
    
    private func stopLoadingAnimation(){
        tipView.hidden=false
        loadingImageView.layer.removeAllAnimations()
    }
    class func refreshView() ->JJRefreshView{
        return NSBundle.mainBundle().loadNibNamed("JJRefreshView", owner: nil, options: nil)!.last as!JJRefreshView
    }
}